package cn.rkylin.oms.common.context;

import static cn.rkylin.apollo.enums.BusinessExceptionEnum.UPDATE_DATA;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.rkylin.core.exception.BusinessException;
import cn.rkylin.oms.system.facade.ResourceBean;
import cn.rkylin.oms.system.project.vo.ProjectVO;
import cn.rkylin.oms.system.role.domain.WF_ORG_ROLE;
import cn.rkylin.oms.system.unit.domain.WF_ORG_UNIT;

/**
 * 用户
 * 
 * @todo 当前登录的用户
 * @author wangxiaoyi
 * @version 1.0
 * @create 2017年2月15日
 */
public class CurrentUser implements Serializable {
    /**
     * 序列号
     */
    private static final long serialVersionUID = -3562152949305285046L;

    /**
     * 主键
     */
    private String id;

    /**
     * 登录帐户名
     */
    private String account;

    /**
     * 全名
     */
    private String fullName;

    /**
     * 密码
     */
    private String password;

    /**
     * 描述
     */
    private String description;

    /**
     * 是否有效
     */
    private boolean enabled;

    /**
     * 是否锁定
     */
    private boolean locked;

    /**
     * 帐号创建日期
     */
    private Date createdDate;

    /**
     * 密码修改日期
     */
    private Date pwdChangeDate;

    /**
     * 角色列表WF_ORG_ROLE
     */
    private List roleList;

    /**
     * 组织单元列表WF_ORG_UNIT
     */
    private List unitList;

    /**
     * 岗位列表WF_ORG_STATION
     */
    private List stationList;

    /**
     * 用户拥有的shopId集合
     */
    private List<String> shopIdList;

    /**
     * 用户【可管理】的项目集合
     */
    private Map<String, ProjectVO> projectMap;

    /**
     * 用户的当前项目，在index.jsp页面上可以切换。
     */
//    private ProjectVO currentProject;

    /**
     * 用户拥有的企业id集合<id,name>
     */
    private Map<String, String> enterpriseMap;

    /**
     * 用户可用的菜单
     */
    private List<ResourceBean> availableMenus;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getPwdChangeDate() {
        return pwdChangeDate;
    }

    public void setPwdChangeDate(Date pwdChangeDate) {
        this.pwdChangeDate = pwdChangeDate;
    }

    public List getRoleList() {
        return roleList;
    }

    public void setRoleList(List roleList) {
        this.roleList = roleList;
    }

    public List getUnitList() {
        return unitList;
    }

    public void setUnitList(List unitList) {
        this.unitList = unitList;
    }

    public List getStationList() {
        return stationList;
    }

    public void setStationList(List stationList) {
        this.stationList = stationList;
    }

    public String getDescription() {
        return description;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public boolean isLocked() {
        return locked;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public List<ResourceBean> getAvailableMenus() {
        return availableMenus;
    }

    public void setAvailableMenus(List<ResourceBean> availableMenus) {
        this.availableMenus = availableMenus;
    }

    public List<String> getShopIdList() {
        return shopIdList;
    }

    public void setShopIdList(List<String> shopIdList) {
        this.shopIdList = shopIdList;
    }

    public Map<String, ProjectVO> getProjectMap() {
        return projectMap;
    }

    public void setProjectMap(Map<String, ProjectVO> projectMap) {
        this.projectMap = projectMap;
    }

    public Map<String, String> getEnterpriseMap() {
        Map<String, String> returnList = new HashMap<String, String>();
        if (unitList == null || unitList.size() == 0) {
            return returnList;
        }
        for (int i = 0; i < unitList.size(); i++) {
            WF_ORG_UNIT unit = (WF_ORG_UNIT) unitList.get(i);
            returnList.put(unit.getEntId(), unit.getEntName());
        }
        return returnList;
    }

    public void setEnterpriseMap(Map<String, String> enterpriseIdMap) {
        this.enterpriseMap = enterpriseIdMap;
    }

    /**
     * 获取用户在index.jsp上切换的当前项目(此方法改用WebContextFactory.getWebContext().getCurrentProject)
     * @return
     * @deprecated
     */
    public ProjectVO getCurrentProject() {
//        return currentProject;
        return WebContextFactory.getWebContext().getCurrentProject();
    }

    /**
     * 设置当前项目(此方法改用WebContextFactory.getWebContext().setCurrentProject)
     * @deprecated
     */
    public void setCurrentProject(ProjectVO currentProject) {
//        this.currentProject = currentProject;
        WebContextFactory.getWebContext().setCurrentProject(currentProject);
    }

    /**
     * 当前用户是否具有管理角色
     * @return true:有，false:没有
     */
    public boolean hasAdminRole() {
        boolean returnValue = false;
        if (this.roleList != null) {
            for (int i = 0; i < this.roleList.size(); i++) {
                WF_ORG_ROLE role = (WF_ORG_ROLE) roleList.get(i);
                if (role.getIsAdminrole() != null && role.getIsAdminrole().equals("是")) {
                    returnValue = true;
                    break;
                }
            }
        }
        return returnValue;
    }

    /**
     * 获取user的adminRole(每个用户应该只能有1个adminRole)
     * 
     * @return user的管理角色
     */
    public WF_ORG_ROLE getAdminRole() {
        WF_ORG_ROLE returnValue = null;
        if (this.roleList != null) {
            for (int i = 0; i < this.roleList.size(); i++) {
                WF_ORG_ROLE role = (WF_ORG_ROLE) roleList.get(i);
                if (role.getIsAdminrole() != null && role.getIsAdminrole().equals("是")) {
                    returnValue = role;
                    break;
                }
            }
        }
        return returnValue;
    }

    public void checkEnterpriseId(String enterpriseId, String message) throws BusinessException {
        if (!getEnterpriseMap().containsKey(enterpriseId)) {
            throw new BusinessException(UPDATE_DATA.getC(), message);
        }
    }
}